import path from "path";
import CopyPlugin from "copy-webpack-plugin";
import { merge } from "webpack-merge";
import packageInfo from "./package.json";
import TerserPlugin from "terser-webpack-plugin";
import JavaScriptObfuscator from "webpack-obfuscator";
import HtmlWebpackPlugin from "html-webpack-plugin";

const config = {
    entry: {
        "index": "./src/index.ts",
    },
    mode: "development",
    devtool: "inline-source-map",
    plugins: [
        new HtmlWebpackPlugin({
            template: "./template/index.html",
            chunks: ["index"],
            filename: "./index.html",
        }),
    ],
    output: {
        clean: true,
        path: path.resolve(__dirname, "dist"),
        filename: "[name].js",
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader",
                include: [path.resolve(__dirname, "src"), path.resolve(__dirname, "demo")],
            },
            {
                test: /\.css$/i,
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: ["url-loader"],
            },

        ],
    },
    devServer: {
        open: true,
        host: "localhost",
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"],
        symlinks: false,
    },
    optimization: {},

};

const dev = process.argv.indexOf("production") < 0;
console.log(`当前生成的是 ${dev ? "可调试" : "不可调试"} 版本`);
console.log(`程序版本号 = ${packageInfo.version}`);

if (dev) {
    config.mode = "development";
    config.devtool = "inline-source-map";
} else {
    config.mode = "production";
    config.devtool = "nosources-source-map";
    // 添加混乱
    (config.plugins as any[]).push(new JavaScriptObfuscator(
        {
            rotateStringArray: true,
            // // 干扰F12
            // debugProtection: true,
            // debugProtectionInterval: 10,
            // disableConsoleOutput: true,
            // //标识符的混淆方式 hexadecimal(十六进制) mangled(短标识符)
            identifierNamesGenerator: "hexadecimal",
            // // 将信息记录到控制台。
            // log: false,
        },
        // 排除的文件
        [],
    ));
    // 添加压缩
    config.optimization = {
        // 开启压缩器
        minimize: true,
        // 指定压缩器
        minimizer: [new TerserPlugin({
            terserOptions: {
                // 删除注释
                format: { comments: false },
                // 默认压缩
                compress: true,
            },
            // 删除注释
            extractComments: false,
        })],
    };
}
module.exports = config;